using UnityEngine;
using System.Collections;

[RequireComponent (typeof(ComponentHealth))]
public class UIHealthBar : MonoBehaviour
{
	public Camera MainCamera;
	public Camera FixedCamera;
	
	public Rect Rectangle = new Rect(0, 0, 50, 4);
	public Vector2 Offset = new Vector2(-25, 0);
	public int playerScore;
	
	Texture2D Background;
	Texture2D Foreground;
	
	ComponentHealth Health;

	// Use this for initialization
	void Start ()
	{
		// Use 1x1 pixels to draw the health bars
		Background = new Texture2D(1,1);
		Background.SetPixel(0,0, Color.red);
		Background.Apply();
		Foreground = new Texture2D(1,1);
		Foreground.SetPixel(0,0, Color.green);
		Foreground.Apply();
		
		Health = GetComponent<ComponentHealth>();
		
		//FixedCamera = Resources.Load("FixedCamera", typeof(Camera));
		//MainCamera = Resources.Load("MainCamera", typeof(Camera));
	}
	
	void OnGUI ()
	{
		if (MainCamera.enabled == true) 
		{
			Rectangle.x = MainCamera.WorldToScreenPoint(transform.position).x + Offset.x;
			Rectangle.y = Screen.height - MainCamera.WorldToScreenPoint(transform.position).y + Offset.y;
		}
		else 
		{
			Rectangle.x = FixedCamera.WorldToScreenPoint(transform.position).x + Offset.x;
			Rectangle.y = Screen.height - FixedCamera.WorldToScreenPoint(transform.position).y + Offset.y;
		}
			
		GUI.DrawTexture(Rectangle, Background);
		
		Rect partialRect = Rectangle;
		partialRect.width = Rectangle.width * (Health.FractionHP);
		partialRect.x = Rectangle.x;
		
		GUI.DrawTexture(partialRect, Foreground);
		
        //GUI.Label(new Rect(10,10,200,50),"HP:" + m_Health.CurrHP);
		//GUI.Label(new Rect(10,60,200,50),"Score:" + playerScore);
        
	}
}
